package com.example.recyclerview01.baseuse;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.recyclerview01.R;

import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private List<String> mData; // 数据源
    private Context mContext;
    public RecyclerAdapter(Context mContext, List<String> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }
    // 回调点击事件三件套。
    private ItemClickListener mItemClickListener;
    public void setOnItemClickListener(RecyclerAdapter.ItemClickListener listener) {
        this.mItemClickListener = listener;
    }
    public interface ItemClickListener {
        void onItemClick(int position);
    }
    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) {
        holder.itemTv.setText(mData.get(position));
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { // 回调点击事件。
                    mItemClickListener.onItemClick(position);
                }
            });
        }
    }


    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.home, parent, false);
        ViewHolder viewHolder = new RecyclerAdapter.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView itemTv;

        public ViewHolder(View itemView) {
            super(itemView);
            itemTv = itemView.findViewById(R.id.id_num);
        }
    }
}